package com.smartTerminal.perMatters.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartTerminal.perMatters.entity.Approval;
import com.smartTerminal.perMatters.entity.Bussiness;
import com.smartTerminal.perMatters.service.MyBussinessService;
import com.smartTerminal.webservice.TerminalServices;

@Service
public class MyBussinessServiceImpl implements MyBussinessService {

	/**
	 * 获取办件列表
	 * @method	queryBussinessList
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param bussiness
	 * @param token
	 * @param currentpage
	 * @param pagesize
	 * @return
	 */
	public List<Bussiness> queryBussinessList(Bussiness bussiness, String token) throws Exception{
		String context = TerminalServices.getInstance().getTerminalServicesHttpPort().
				queryBussinessList(token, bussiness.getState()==null?"":bussiness.getState(), 
				bussiness.getItemName()==null?"":bussiness.getItemName(), 
						String.valueOf(bussiness.getCurrentpage()),
						String.valueOf(bussiness.getPagesize())).toLowerCase();
		if(!"0".equals(JSONObject.parseObject(context).getString("code"))){
			throw new Exception("服务器异常,请联系管理员");
		}
		JSONObject json = JSONObject.parseObject(context).getJSONObject("data");
		int currentRow = (int) Math.ceil(Double.valueOf(json.getIntValue("rows"))/Double.valueOf(bussiness.getPagesize()));
		bussiness.setCurrentRows(currentRow);
		List<Bussiness> list = JSONArray.parseArray(json.getString("data"), Bussiness.class);
		return list;
	}
	
	/**
	 * 获取办件详情
	 * @method	getBusinessById
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param bussiness
	 * @return
	 */
	public Bussiness getBusinessById(Bussiness bussiness) throws Exception {
		String context = TerminalServices.getInstance().getTerminalServicesHttpPort().
				getBusinessById(bussiness.getProkeyid()).toLowerCase();
		if(!"0".equals(JSONObject.parseObject(context).getString("code"))){
			throw new Exception("服务器异常,请联系管理员");
		}
		String json = JSONObject.parseObject(context).getString("data");
		bussiness = JSONObject.parseObject(json, Bussiness.class);
		return bussiness;
	}
	
	/**
	 * 获取办事进度
	 * @method	getBusinessInfo
	 * @author	Xia.Zhuming
	 * @data	2018年1月16日
	 * @param approval
	 * @return
	 */
	public List<Approval> getBusinessInfo(String order) throws Exception {
		String context = TerminalServices.getInstance().getTerminalServicesHttpPort().
				getBusinessInfo(order).toLowerCase();
		if(!"0".equals(JSONObject.parseObject(context).getString("code"))){
			throw new Exception("服务器异常,请联系管理员");
		}
		String json = JSONObject.parseObject(context).getString("data");
		List<Approval> list = JSONArray.parseArray(json, Approval.class);
		return list;
	}
}
